<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_model extends MY_Model
{
public function userList($data = array(),$per_page,$limit){
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $where .= " AND createtime <= {$data['endtime']}+3600*24";
        }
        if(!empty($data['keyword'])){
            $where .= " AND username like '%{$data['keyword']}%' OR  AccountID LIKE '%{$data['keyword']}%' OR nickname LIKE '%{$data['keyword']}%'";
        }
        $limit = "LIMIT {$per_page},{$limit}";
        $sql = "SELECT
                    *
            FROM
                   blues_user 
            where 
                status<>2 {$where} ORDER BY createtime desc {$limit} ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    public function get_user_cnt($data = array()){
        $where = " WHERE 1 ";
        if(!empty($data['starttime'])){
            $where .= " AND createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $where .= " AND createtime <= {$data['endtime']}+3600*24";
        }
        if(!empty($data['name'])){
            $where .= " AND username like '%{$data['keyword']}%' OR  AccountID LIKE '%{$data['keyword']}%' OR nickname LIKE '%{$data['keyword']}%'";
        }
        $sql = "SELECT
                    *
            FROM
                   blues_user   
            where 
                status<>2 ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }




/**
 * 修改用户停用启用状态
 * @param  [type] $table [description]
 * @param  array  $data  [description]
 * @return [type]        [description]
 */
   public function editStatus($AccountID,$status){
        $this->db->where("AccountID",$AccountID);
		if($status == -1)
		{
			$this->db->trans_start();
			$this->db->where("AccountID",$AccountID);
			$this->db->delete("blues_token");
			$this->db->where("AccountID",$AccountID);
			$res = $this->db->delete("blues_user");
			$this->db->where("AccountID",$AccountID);
			$res = $this->db->delete("blues_third_login");
			$this->db->where("AccountID",$AccountID);
			$res = $this->db->delete("blues_member_points");
			$this->db->trans_complete();
			$res = true;
			if ($this->db->trans_status() === FALSE)
			{
				$res = false;
			}
		}else{
			$datas = array(
				'Status'=>$status
			);
			$res = $this->db->update("blues_user",$datas);
            $this->db->where("AccountID",$AccountID);
            $this->db->update("blues_token",['token'=>""]);
		}
       
        return $res;
    }  

  public function editUser($AccountID = 0, $data = [])
    {
        if($AccountID){
             //修改
            $this->db->where("AccountID",$AccountID);
            $res = $this->db->update("blues_user", $data);
        }else{
            $checkusername = $this->check_username($data['username']);
            if($checkusername)return false ;

            $this->db->insert("blues_user", $data);
            $res = $this->db->insert_id();
        }
        return $res;
    } 


    public function check_username($username)
    {
        $sql = "SELECT * FROM blues_user WHERE username = '$username'";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

}


